package de.justphil.tcg.tcgserver.game.domain;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Locale;


import com.hazelcast.nio.DataSerializable;

import de.justphil.tcg.tcgserver.commons.config.I18nConfig;
import de.justphil.tcg.tcgserver.websockets.TCGWebSocket;
import de.justphil.tcg.tcgserver.websockets.WebSocketApplicationHolder;
import de.justphil.tcg.tcgserver.websockets.interfaces.TCGAppManageable;

public class Player implements DataSerializable {
	
	private static final long serialVersionUID = -4228397841580789875L;
	
	private long playerId;
	private TCGWebSocket socket;
	private Locale locale;
	private int index;
	private String otp;

	public Player() {
		// just to conform the DataSerializable interface
	}
	
	public Player(long playerId, int index, String otp) {
		this(playerId, index, otp, new Locale(I18nConfig.DEFAULT_LANGUAGE, I18nConfig.DEFAULT_COUNTRY));
	}
	
	public Player(long playerId, int index, String otp, Locale locale) {
		this.playerId = playerId;
		this.locale = locale;
		this.index = index;
		this.otp = otp;
	}
	
	/* ############################################################################################ */
	/* ############################################################################################ */
	/* ############################################################################################ */
	
	@Override
	public void readData(DataInput in) throws IOException {
		playerId = in.readLong();
		
		String lang		= in.readUTF();
		String country	= in.readUTF();
		locale = new Locale(lang, country);
		
		index = in.readInt();
		otp = in.readUTF();
		
		TCGAppManageable tcgApp = WebSocketApplicationHolder.getTCGApplication();
		socket = tcgApp.getWebSocketByPlayerId(playerId);
	}

	@Override
	public void writeData(DataOutput out) throws IOException {
		out.writeLong(playerId);
		out.writeUTF(locale.getLanguage());
		out.writeUTF(locale.getCountry());
		out.writeInt(index);
		out.writeUTF(otp);
	}
	
	/* ############################################################################################ */
	/* ############################################################################################ */
	/* ############################################################################################ */

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (playerId ^ (playerId >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		
		if (obj == null)
			return false;
		
		if (getClass() != obj.getClass())
			return false;
		
		Player other = (Player) obj;
		
		if (playerId != other.playerId)
			return false;
		
		return true;
	}
	
	/* ############################################################################################ */
	/* ############################################################################################ */
	/* ############################################################################################ */
	
	public long getPlayerId() {
		return playerId;
	}

	public TCGWebSocket getSocket() {
		return socket;
	}
	
	public void setSocket(TCGWebSocket socket) {
		this.socket = socket;
	}
	
	public boolean isConnected() {
		return (socket != null && socket.isConnected());
	}
	
	/*
	public boolean isConnectionProblem() {
		return (!isConnected() || socket.isConnectionProblem());
	}
	
	public boolean isReadyToPlay() {
		return (isConnected() && !isConnectionProblem());
	}
	*/

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	public int getIndex() {
		return index;
	}

	public String getOtp() {
		return otp;
	}

}
